package models;

import play.*;
import play.db.jpa.*;

import javax.persistence.*;
import java.util.*;

/**
 * RelatedEntities.java Purpose: Model
 * 
 * @version 1.0 3/5/2011
 * @author Yomna ElRashidi
 */
@Entity
public class RelatedEntities extends Model {

	@ManyToOne
	public EEntity firstEntity;

	@ManyToOne
	public EEntity secondEntity;

	public String relationName;

	public String description;

	/**
	 * constructs a RelatedEntities record for the creation of a relation
	 * between 2 entities
	 * 
	 * @author Yomna El Rashidi
	 * 
	 * @param firstEntity
	 *            The entity in first side of the relation
	 * @param secondEntity
	 *            The entity in the second side of the relation
	 * @param relationName
	 *            The name of the relation between the entities
	 * @param description
	 *            The description of the relation
	 */

	public RelatedEntities(EEntity firstEntity, EEntity secondEntity,
			String relationName, String description) {
		this.firstEntity = firstEntity;
		this.secondEntity = secondEntity;
		this.relationName = relationName;
		this.description = description;
	}

	/**
	 * renames the relations between entities
	 * 
	 * @author Yomna El Rashidi
	 * @param relationID
	 *            the ID of the relation to be renamed.
	 * @param newName
	 *            the new name to be inserted.
	 * */
	public static void renameRelation(long relationID, String newName) {
		RelatedEntities relation = RelatedEntities.find("byID", relationID)
				.first();
		relation.relationName = newName;
		relation.save();

	}

	/**
	 * deletes a relation between entities in an organization
	 * 
	 * @author Yomna El Rashidi
	 * @param relationID
	 *            the ID of the relation we want to rename
	 ***/

	public static void deleteRelation(long relationID) {
		RelatedEntities relation = RelatedEntities.find("byID", relationID)
				.first();
		relation.cascadeDelete();
	}

	/**
	 * create the relations between entities within an organization
	 * 
	 * @author Yomna El Rashidi
	 * 
	 * @param toRelate1
	 *            the first entity to create a relation with.
	 * @param toRelate2
	 * 			the second entity to create the relation with.
	 * @param name
	 *            the name of the entity.
	 * @param description
	 *            the description of the relation.
	 * */
	public static void createRelation(EEntity toRelate1, EEntity toRelate2,
			String name, String description) {
		RelatedEntities newRelation = new RelatedEntities(toRelate1, toRelate2,
				name, description).save();
		newRelation.save();
		toRelate1.relations.add(newRelation);
		toRelate2.relatesTo.add(newRelation);
		toRelate1.save();
		toRelate2.save();
	}

	/**
	 * @author Ahmed Mahmoud
	 * @return
	 * 
	 */
	public boolean cascadeDelete() {
		firstEntity.relatesTo.remove(this);
		firstEntity.relations.remove(this);
		secondEntity.relatesTo.remove(this);
		secondEntity.relations.remove(this);
		
		firstEntity.save();
		secondEntity.save();
	
		this.delete();
		return true;
	}

}
